package com.mz.ieann.aide.base.vb

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentActivity
import androidx.viewbinding.ViewBinding
import com.mz.ieann.aide.R
import com.mz.ieann.dokit.utils.LogUtils

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by zhangmz90
 * @date on 2021 2021/11/14 13:39
 */
open class BaseVBDialog @JvmOverloads constructor(@LayoutRes val resId: Int = 0) : DialogFragment() {

    /**
     * ViewBinding
     */
    inline fun <reified VB : ViewBinding> bindView() = ViewBindingAchieve(VB::class.java)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 背景透明
        setStyle(STYLE_NORMAL, R.style.BaseDialog)
    }

    /**
     * 不自定义布局，则用系统默认
     */
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return when{
            resId <= 0 -> {
                activity?.let {
                    val build = AlertDialog.Builder(it)
                    build.setMessage(R.string.app_name)
                        .setPositiveButton("确认", DialogInterface.OnClickListener{ dialog, which ->

                        })
                        .setNegativeButton("取消", DialogInterface.OnClickListener{ dialog, which ->

                        })
                    build.create()
                } ?: throw IllegalStateException("activity can not be null")
            }
            else -> super.onCreateDialog(savedInstanceState)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.setCanceledOnTouchOutside(false)
        setAnimations()
        return when{
            resId <= 0 -> super.onCreateView(inflater, container, savedInstanceState)
            else -> inflater.inflate(resId, container, false)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initCreate()
    }

    open fun initCreate(){

    }

    protected fun bindSure(view: View, ok: ()-> Unit = {  }){
        view.setOnClickListener{
            this.dismiss()
            ok()
        }
    }

    protected fun bindCancel(view: View){
        view.setOnClickListener{
            this.dismiss()
        }
    }

    open fun show(activity: FragmentActivity?, tag: String){
        activity?.apply {
            show(supportFragmentManager, tag)
        }?: LogUtils.e("activity can not be null")
    }

    fun showNow(activity: FragmentActivity?, tag: String){
        activity?.apply {
            showNow(supportFragmentManager, tag)
        }?: LogUtils.e("activity can not be null")
    }

    /**
     * 设置位置
     */
    fun setGravity(g: Gravity){
//        dialog?.window?.apply {
//            val param = attributes
//            param?.gravity = g
//            attributes = param
//        }
    }

    private fun setAnimations(){
//        dialog?.window?.setWindowAnimations(R.anim.media_fade_in)
    }

    override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
    }

    override fun onDetach() {
        super.onDetach()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

}